﻿using Md.Api.Entity.DapperModels.MdDB;

namespace Md.Dapper.Repositories.Md;
/// <summary>
/// 文章字段扩展仓储
/// </summary>
public class MdArticleFieldExpansionRep : BaseRep<MdArticleFieldExpansionModel>
{
    private static string sqlFields = string.Format(@"
        `article_expan_id`,
        `article_id`,
        `field_name`,
        `field_value`,
        `field_type`,
        `field_options`,
        `category_id`,
        `status`,
        `del_flag`,
        `create_by`,
        `create_time`,
        `update_by`,
        `update_time`,
        `remark`
    ");

    /// <summary>
    /// 通过编号获取文章字段扩展详情
    /// </summary>
    /// <param name="id">编号</param>
    /// <returns>MdArticleFieldExpansionModel</returns>
    public MdArticleFieldExpansionModel? GetMdArticleFieldExpansionById(long id)
    {
        string sqlWhere = string.Format(@" and `del_flag` = '0' and `article_expan_id` = @ArticleExpanId ");

        return QuerySingle(sqlFields, sqlWhere, new { ArticleExpanId = id });
    }

    /// <summary>
    /// 获取文章字段扩展列表
    /// </summary>
    /// <param name="model">查询条件</param>
    /// <param name="orderBy">排序字段</param>
    /// <param name="isDesc">是否倒叙</param>
    /// <returns>IEnumerable<MdArticleFieldExpansionModel></returns>
    public IEnumerable<MdArticleFieldExpansionModel> GetMdArticleFieldExpansionList(MdArticleFieldExpansionModel model, string orderBy, bool isDesc)
    {
        string sqlWhere = string.Empty;

        #region ===where===
        if (model.ArticleId > 0)
        {
            sqlWhere += " and `article_id` = @ArticleId ";
        }
        if (!string.IsNullOrWhiteSpace(model.FieldName))
        {
            sqlWhere += " and `field_name` like concat('%', @FieldName, '%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.FieldType))
        {
            sqlWhere += " and `field_type` = @FieldType ";
        }
        if (model.CategoryId > 0)
        {
            sqlWhere += " and `category_id` = @CategoryId ";
        }
        if (!string.IsNullOrWhiteSpace(model.Status))
        {
            sqlWhere += " and `status` = @Status ";
        }
        if (!string.IsNullOrWhiteSpace(model.CreateBy))
        {
            sqlWhere += " and `create_by` = @CreateBy ";
        }
        if (!string.IsNullOrWhiteSpace(model.UpdateBy))
        {
            sqlWhere += " and `update_by` = @UpdateBy ";
        }
        sqlWhere += " and `del_flag` = '0' ";
        sqlWhere += GetOrderByDesc(orderBy, isDesc);
        #endregion

        return QueryList(sqlFields, sqlWhere, model);
    }

    /// <summary>
    /// 分页获取文章字段扩展列表
    /// </summary>
    /// <param name="model">查询条件</param>
    /// <param name="pageIndex">当前页</param>
    /// <param name="pageSize">每页条数</param>
    /// <param name="orderBy">排序字段</param>
    /// <param name="isDesc">是否倒序</param>
    /// <returns>Page<MdArticleFieldExpansionModel></returns>
    public Page<MdArticleFieldExpansionModel> GetMdArticleFieldExpansionPageList(MdArticleFieldExpansionModel model, int pageIndex, int pageSize, string orderBy, bool isDesc)
    {
        string sqlWhere = string.Empty;

        #region ===where===
        if (model.ArticleId > 0)
        {
            sqlWhere += " and `article_id` = @ArticleId ";
        }
        if (!string.IsNullOrWhiteSpace(model.FieldName))
        {
            sqlWhere += " and `field_name` like concat('%', @FieldName, '%') ";
        }
        if (!string.IsNullOrWhiteSpace(model.FieldType))
        {
            sqlWhere += " and `field_type` = @FieldType ";
        }
        if (model.CategoryId > 0)
        {
            sqlWhere += " and `category_id` = @CategoryId ";
        }
        if (!string.IsNullOrWhiteSpace(model.Status))
        {
            sqlWhere += " and `status` = @Status ";
        }
        if (!string.IsNullOrWhiteSpace(model.CreateBy))
        {
            sqlWhere += " and `create_by` = @CreateBy ";
        }
        if (!string.IsNullOrWhiteSpace(model.UpdateBy))
        {
            sqlWhere += " and `update_by` = @UpdateBy ";
        }
        sqlWhere += " and `del_flag` = '0' ";
        sqlWhere += GetOrderByDesc(orderBy, isDesc);
        #endregion

        return QueryPage(sqlFields, sqlWhere, model, pageIndex, pageSize);
    }

    /// <summary>
    /// 通过编号更新文章字段扩展
    /// </summary>
    /// <param name="model">请求实体</param>
    /// <returns>long</returns>
    public long UpdateMdArticleFieldExpansionById(MdArticleFieldExpansionModel model)
    {
        var sqlSet = string.Empty;

        #region ===set===
        if (model.ArticleId > 0)
        {
            sqlSet += " `article_id` = @ArticleId, ";
        }
        if (!string.IsNullOrWhiteSpace(model.FieldName))
        {
            sqlSet += " `field_name` = @FieldName, ";
        }

        if (!string.IsNullOrWhiteSpace(model.FieldValue))
        {
            sqlSet += " `field_value` = @FieldValue, ";
        }

        if (!string.IsNullOrWhiteSpace(model.FieldType))
        {
            sqlSet += " `field_type` = @FieldType, ";
        }

        if (model.FieldOptions != null)
        {
            sqlSet += " `field_options` = @FieldOptions, ";
        }

        if (model.CategoryId > 0)
        {
            sqlSet += " `category_id` = @CategoryId, ";
        }

        if (model.Remark != null)
        {
            sqlSet += " `remark` = @Remark, ";
        }

        if (!string.IsNullOrWhiteSpace(model.Status))
        {
            sqlSet += " `status` = @Status, ";
        }

        if (!string.IsNullOrWhiteSpace(model.UpdateBy))
        {
            sqlSet += " `update_by` = @UpdateBy, ";
        }

        sqlSet += " `update_time` = sysdate() ";
        #endregion

        string sqlWhere = " and `del_flag` = '0' and `article_expan_id` = @ArticleExpanId ";

        return Update(sqlSet, sqlWhere, model);
    }

    /// <summary>
    /// 通过编号删除文章字段扩展
    /// </summary>
    /// <param name="ids">编号多个逗号分隔</param>
    /// <returns>int</returns>
    public int DeleteMdArticleById(string ids)
    {
        string sqlWhere = " and `article_expan_id` in @ArticleExpanIds ";

        return Delete(sqlWhere, new { ArticleExpanIds = ids.Split(',').ToArray() });
    }
}

